#include "qipan.h"
#include <QPainter>
#include <QDebug>
#include <QPoint>
qipan::qipan(QWidget *parent) : QWidget(parent)
{
    m_hoverFlag = false;
    m_board = (Role **)malloc(sizeof(Role *)*m_boardSize);
    for(int idx =0;idx<m_boardSize;idx++)
    {
        m_board[idx]= (Role*)malloc(sizeof(int*)*m_boardSize);
    }
    for(int row = 0;row < m_boardSize;row++)
    {
       for(int col=0;col<m_boardSize;col++)
       {
           m_board[row][col] = EMPTY;
       }
    }
    /*清除脏数据*/
//    memset(m_board,0,sizeof(m_board));
//    m_board.clear;
    /*捕获鼠标移动*/
    setMouseTracking(true);
}

void qipan::mouseMoveEvent(QMouseEvent *event)
{
//    int posX = event->x();
//    int posY = event->y();
//    qDebug()<<"posX:"<<posX;
//    qDebug()<<"posY:"<<posY<<endl;
//    int row = (posX-m_margin+m_cellSize/2)/m_cellSize;
//    int col = (posY-m_margin+m_cellSize/2)/m_cellSize;
//    qDebug()<<"row:"<<row;
//    qDebug()<<"col:"<<col<<endl;
//    update();
    int row = (event->x()-m_margin+m_cellSize/2)/m_cellSize;
    int col = (event->y()-m_margin+m_cellSize/2)/m_cellSize;
    /*判断是否在棋盘内部*/
    if((row >= 0 && row<m_boardSize)&&(col >= 0 && col <m_boardSize))
    {
     if(m_hoverRow != row || m_hoverCol !=col)
        {
            m_hoverRow = row;
            m_hoverCol = col;
            m_hoverFlag= true;
            /*更新画面*/
            update();
        }
    }
    else
    {
        if(m_hoverFlag)
        {
            m_hoverFlag = false;
            /*移出棋盘需要将坐标-1*/
            m_hoverRow = m_hoverCol-1;
            /*更新画面*/
            update();

        }
    }
}
/*鼠标点击*/
void qipan::mousePressEvent(QMouseEvent *event)
{
    int row = (event->x()-m_margin+m_cellSize/2)/m_cellSize;
    int col = (event->y()-m_margin+m_cellSize/2)/m_cellSize;
    /*判断是否在棋盘内部*/
    if((row >= 0 && row<m_boardSize)&&(col >= 0 && col <m_boardSize) && m_board[row][col] == EMPTY)
    {
        m_board[row][col] = PLATER;
        update();
        emit chessClick(row,col);
    }

}
/*记录i棋盘位置对应的玩家*/
void qipan::setBoardInfo(int row, int col, Role role)
{
    if((row >= 0 && row<m_boardSize)&&(col >= 0 && col <m_boardSize) && m_board[row][col] == EMPTY)
    {
        m_board[row][col] = role;
    }
}
/*得到棋盘信息*/
Role **qipan::getBoardInfo(int &boardSize)const
{
    boardSize = m_boardSize;
    return reinterpret_cast<Role**>(m_board);
}

int qipan::getBoardSize()
{
    return m_boardSize;
}

void qipan::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    /*启用抗锯齿效果*/
    painter.setRenderHint(QPainter::Antialiasing);
    // 绘制棋盘背景
        // 棋盘背景颜色
        QColor backgroundColor = QColor(255,192,203); // 淡灰色
        painter.fillRect(rect(), backgroundColor);
    /*绘制棋盘的网格线*/
    for(int idx=0;idx<m_boardSize;idx++)
    {
        /*绘制竖线*/
        painter.drawLine(m_margin+idx*m_cellSize,m_margin,m_margin+idx*m_cellSize,m_margin+(m_boardSize-1)*m_cellSize);
        /*绘制横线*/
        painter.drawLine(m_margin,m_margin+idx*m_cellSize,m_margin+(m_boardSize-1)*m_cellSize,m_margin+idx*m_cellSize);
    }
    /*绘制悬停点*/
    if(m_hoverFlag &&
            (m_hoverRow >=0 && m_hoverRow <m_boardSize)&&
            (m_hoverCol >=0 &&m_hoverCol <m_boardSize)&&
            (m_board[m_hoverRow][m_hoverCol] == EMPTY))
    {
        painter.setBrush(Qt::red);
        painter.drawEllipse(QPoint(m_margin + m_hoverRow*m_cellSize,m_margin+m_hoverCol*m_cellSize),m_cellSize/5,m_cellSize/5);
    }
    /*绘制棋子*/
        for(int row = 0;row < m_boardSize;row++)
        {
            for(int col = 0;col < m_boardSize;col++)
            {
               if(m_board[row][col] == PLATER)
               {
                   painter.setBrush(Qt::black);
                   painter.drawEllipse(QPoint(m_margin + row * m_cellSize,m_margin + col * m_cellSize),m_cellSize / 3,m_cellSize / 3);
               }
               else if(m_board[row][col] == COMPUTER)
               {
                   painter.setBrush(Qt::white);
                   painter.drawEllipse(QPoint(m_margin + row * m_cellSize,m_margin + col * m_cellSize),m_cellSize / 3,m_cellSize / 3);
               }
            }
        }

}
